package net.yb.controller;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.yb.entity.User;
import net.yb.service.UserService;
import net.yb.vo.AjaxResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin // 允许跨域访问
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 查询所有用户
     */
    @GetMapping("/list")
    public AjaxResult list(){
        List<User> list = userService.findAll();
        return AjaxResult.success(list);
    }

    /**
     * 根据id删除用户
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult removeById(@PathVariable int id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/removeBatch")
    public AjaxResult removeBatch(@RequestBody List<Integer> ids){
        // userService.removeBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/queryByPage")
    public AjaxResult queryByPage(Integer pageNum, Integer pageSize){
        PageInfo<User> pageInfo = userService.queryByPage(pageNum, pageSize);
        return AjaxResult.success(pageInfo);
    }

}
